#include "common/xmlparser.h"

#include "common/settings.h"
#include "common/log/QsLog.h"

#include <QVector>

/**
  *
  */
XmlParser::XmlParser()
{
}

/**
  *
  */
XmlParser& XmlParser::getInstance()
{
   static XmlParser xml;
   return xml;
}

/**
  *
  */
void XmlParser::writeXml(QString name, QString path,  Parameters configuration){
    QLOG_INFO() << " XML WRITE "<< name << " to " << path;
    QDomDocument doc(name);
    QDomElement root = doc.createElement(name);
    doc.appendChild(root);

    QMapIterator<QString, QMap<QString, float>  > isection(configuration);
    while (isection.hasNext()) {
        isection.next();
        QMapIterator<QString, float> iparameter = isection.value();
        QDomElement tag = doc.createElement(isection.key());
        root.appendChild(tag);
        while(iparameter.hasNext()){
            iparameter.next();
            QDomElement tag1 = doc.createElement(iparameter.key());
            tag.appendChild(tag1);
            QDomText t = doc.createTextNode(QString::number(iparameter.value()));
            tag1.appendChild(t);
        }
    }

    QFile file( QString(path).append("/").append(name).append(".xml") );

    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return;

    QTextStream out(&file);
    out << doc.toString();
    file.close();
}

/**
  *
  */
QVector<int> XmlParser::searchColor(QString color){
    QVector<int> colorForWidgets(0);
    QString path;
    path.append(workspace_directory()).append("/colours.xml");
    QDomDocument doc("mycolours");
    QFile file(path);
    if (!file.open(QIODevice::ReadOnly))
        QLOG_ERROR() <<  "Error al abrir el archivo xml" <<  path;
    if (!doc.setContent(&file)) {
        file.close();
        QLOG_ERROR() <<  "Error al parsear contenido al xml" <<  path;
    }
    file.close();
    QMap<QString,float> parameterMap;
    QDomElement docElem = doc.documentElement();
    QDomNode colors = docElem.firstChild();
    while(!colors.isNull()) {
        QDomElement colorElement = colors.toElement();
        if( (!colorElement.isNull()) &&  (colorElement.tagName().compare(color) == 0)) {
            QDomNode parameter = colorElement.firstChildElement();

            while(!parameter.isNull()) {
                QDomElement parameterDomElement = parameter.toElement();
                QString parameterValue = parameterDomElement.firstChild().nodeValue();
                colorForWidgets.append(parameterValue.toFloat());
                parameter = parameter.nextSibling();
            }
            parameterMap.clear();
        }
        colors = colors.nextSibling();
    }
    return colorForWidgets;
}

/**
  *
  */
Parameters* XmlParser::readXml(QString name, QString path)
{
    QLOG_INFO() << "Read xml file: " << name << " from " << path;

    Parameters *configurationMap = new Parameters;
    QMap<QString,float> parameterMap;
    QString fileDir = QString(path).append("/").append(name);

    QDomDocument doc("mydocument");

    QFile file(fileDir);
    if (!file.open(QIODevice::ReadOnly))
        QLOG_ERROR() <<  "Error al abrir el archivo xml" <<  fileDir;
    if (!doc.setContent(&file)) {
        file.close();
        QLOG_ERROR() <<  "Error al parsear contenido al xml" <<  fileDir;
    }
    file.close();

    QDomElement docElem = doc.documentElement();
    QDomNode section = docElem.firstChild();
    while(!section.isNull()) {
        QDomElement sectionDomElement = section.toElement();
        if(!sectionDomElement.isNull()) {
            QDomNode parameter = sectionDomElement.firstChildElement();
            while(!parameter.isNull()) {
                QDomElement parameterDomElement = parameter.toElement();
                QString parameterName = parameterDomElement.tagName();
                QString parameterValue = parameterDomElement.firstChild().nodeValue();
                parameterMap[parameterName] = parameterValue.toFloat();
                parameter = parameter.nextSibling();
            }
            (*configurationMap)[sectionDomElement.tagName()] = parameterMap;
            parameterMap.clear();
        }
        section = section.nextSibling();
    }

    return configurationMap;
}

